HTML 实现随意拖动内容位置
css 代码
css 代码

2. canvas绘制文本框,并实现鼠标将其拖拽移动到任意位置
<p> <canvas id="canvas" class="cus-canvas" width="800" height="600"></canvas> <input id="inputEle" class="input-ele"/> </p>js代码
实现原理为记录鼠标移动的位置,不断的重绘矩形框和文本内容
#range { position: relative; width: 600px; height: 400px; margin: 10px; background-color: rgb(133, 246, 250); } .icon { position: absolute; height: 100px; width: 100px; cursor: move; background-color: #ff9204; user-select: none; }html代码
1. 实现鼠标拖动标签元素到任意位置
<p id="range"> <p class="icon">100*100</p> </p>js代码
.cus-canvas{ background: rgb(50, 204, 243); } .input-ele{ display: none; position: fixed; width: 180px; border: 0; background-color: #fff; }html 代码
let mouseDown = false; let deltaX = 0; let deltaY = 0; let text = 'hello' const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const cw = canvas.width, ch = canvas.height; const rect = { x: 20, y: 20, width: 150, height: 50 } /** 设置文字和边框样式 */ ctx.font="16px Arial"; ctx.fillStyle = "#fff"; /** 设置为 center 时,文字段的中心会在 fillText的 x 点 */ ctx.textAlign = 'center'; ctx.lineWidth = '2'; ctx.strokeStyle = '#fff'; strokeRect() const inputEle = document.getElementById('inputEle'); inputEle.onkeyup = function(e) { if(e.keyCode === 13) { text = inputEle.value strokeRect() inputEle.setAttribute('style', `display:none`) } } canvas.ondblclick = function(e){ inputEle.setAttribute('style', `left:${e.clientX}px;top:${e.clientY}px;display:block`); inputEle.focus(); } canvas.onmousedown = function(e){ /** 获取视口左边界与canvas左边界的距离 加上 鼠标点击位置与canvas左边界的长度,这个值是相对移动过程中不变的值 */ deltaX=e.clientX - rect.x; deltaY=e.clientY - rect.y; mouseDown = true }; const judgeW = cw-rect.width, judgeH = ch-rect.height; canvas.onmousemove = function(e){ if(mouseDown) { /** 相减即可获得矩形左边界与canvas左边界之间的长度 */ let dx = e.clientX-deltaX; let dy = e.clientY-deltaY; if(dx < 0) { dx = 0; } else if (dx > judgeW) { dx = judgeW; } if(dy < 0) { dy = 0; } else if(dy > judgeH) { dy = judgeH; } rect.x = dx; rect.y = dy; strokeRect() } }; canvas.onmouseup = function(e){ mouseDown = false }; /** 清除指定区域 */ function clearRect() { ctx.clearRect(0, 0, cw, ch) } /** 画矩形 */ function strokeRect() { clearRect() /** 这里如果不调用 beginPath 历史的矩形会重新被绘制 */ ctx.beginPath() ctx.rect(rect.x, rect.y, rect.width, rect.height) ctx.stroke(); // 设置字体内容,以及在画布上的位置 ctx.fillText(text, rect.x + 70, rect.y + 30); }推荐教程:《HTML教程》
以上就是HTML 实现随意拖动内容位置的详细内容,更多请关注聚合云库其它相关文章!
两种方式为:拖拽普通标签位置或拖拽canvas中的文本框位置
相关热词: HTML
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/cssm/4890.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
其中border-left决定了底部直
时间:2021-01-23
-
当你自己回头来看你写的
时间:2021-01-23
-
④格式标签 粗体:b/b 斜
时间:2021-01-23
-
我们直接看代码: !DOCTY
时间:2021-01-23
-
这里就是吐槽的IE6!) 图
时间:2021-01-23
-
假设我们的HTML代码如下:
时间:2021-01-23
-
那么使用 CSS3 新增的选择
时间:2021-01-23
-
scaleGlassRectangle.y
时间:2021-01-23
热门文章
-
可以加我的HTML5前端交流群111645711 CSS源码
时间:2021-01-15
-
就可以对子元素进行 3D 变形操作了
时间:2021-01-12
-
用css让一个容器水平垂直
时间:2021-01-12
-
而没有设置高度
时间:2021-01-19
-
canvas与html5实现视频截图成果
时间:2021-01-19
-
所以通常不需要发送
时间:2021-01-19
-
我们尝试一下更新一下HTML结构
时间:2021-01-23
-
scaleGlassRectangle.y
时间:2021-01-23
-
HTML5生拖放实例分析
时间:2021-01-12
-
在全局:root{ }伪类中定义了一个 CSS 变量
时间:2021-01-21
